package com.phaeton.beemeup.util;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.PinchZoomDetector;
import org.andengine.input.touch.detector.PinchZoomDetector.IPinchZoomDetectorListener;

public class PinchZoomer implements IPinchZoomDetectorListener{
	private ZoomCamera mCam;
	private float mPinchZoomStartedCameraZoomFactor;
	private float maxZoomFactor = 1.65f;
	private float minZoomFactor = .55f;
	
	public PinchZoomer(ZoomCamera cam) {
		mCam = cam;
	}

	@Override
	public void onPinchZoomStarted(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent) {
		this.mPinchZoomStartedCameraZoomFactor = this.mCam.getZoomFactor();
	}

	@Override
	public void onPinchZoom(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor) {
		if(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor < maxZoomFactor 
				&& this.mPinchZoomStartedCameraZoomFactor * pZoomFactor > minZoomFactor)
			this.mCam.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor);
	}

	@Override
	public void onPinchZoomFinished(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor) {
		if(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor < maxZoomFactor 
				&& this.mPinchZoomStartedCameraZoomFactor * pZoomFactor > minZoomFactor)
			this.mCam.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor);
	}
}
